#include "Rp2LightingMaterialEffect.h"

using namespace Rp2;

RP2_IMPLEMENT_RTTI(Rp2, LightingMaterialEffect, LightingEffect);
RP2_IMPLEMENT_DEFAULT_NAME_ID(LightingMaterialEffect, LightingEffect);

//---------------------------------------------------------------------------------------------------
LightingMaterialEffect::LightingMaterialEffect(const std::string& rkTextureName)
	:
	LightingEffect(1)
{
	m_kTexName = rkTextureName;
}
//---------------------------------------------------------------------------------------------------
LightingMaterialEffect::~LightingMaterialEffect()
{
}
//---------------------------------------------------------------------------------------------------
void LightingMaterialEffect::Configure()
{
    if (m_kLights.size() == 0)
    {
        SetPassQuantity(1);
        m_kVShader[0] = new VertexShader("Texture");
        m_kPShader[0] = new PixelShader("Texture");
		m_kPShader[0]->SetTexture(0, m_kTexName);
        return;
    }

	// Use a bucket sort on the lights to arrange them in the order:
    // ambient, directional, point, spot.
	std::vector<LightPtr> m_akBucket[4];
	int iLQuantity = (int)m_kLights.size();
	int i, iType;
	for (i = 0; i < iLQuantity; i++)
	{
		iType = (int)m_kLights[i]->Type;
		m_akBucket[iType].push_back(m_kLights[i]);
	}

	std::string kShaderName("LitMat_");

	// set texture
	if (m_kTexName != "")
	{
		kShaderName += std::string("t_");
	}

	const std::string acType[4] = {"a", "d", "p", "s"};
	for (iType = 0; iType < 4; iType++)
	{
		for (i = 0; i < (int)m_akBucket[iType].size(); i++)
		{
			kShaderName += acType[iType];
		}
	}

	// set shader
	if (!m_kVShader[0])
	{
		m_kVShader[0] = new VertexShader(kShaderName);
	}
	else
	{
		m_kVShader[0]->SetShader(kShaderName);
	}

	if (!m_kPShader[0])
	{
		m_kPShader[0] = new PixelShader(kShaderName);
	}
	else
	{
		m_kPShader[0]->SetShader(kShaderName);
	}

	// set texture
	m_kPShader[0]->SetTexture(0, m_kTexName);


}
//---------------------------------------------------------------------------------------------------